<template>
  <div>
    <!--    查询功能-->
    <div style="margin-bottom: 5px">
      <!--    输入学院名称查询-->
      <el-input
        v-model="searchForName"
        @keyup.enter.native="loadPost"
        placeholder="请输入头衔名称"
        suffix-icon="el-icon-search"
        style="width: 200px"
      ></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="loadPost"
        >名称查询</el-button
      >
      <!-- 输入学院id查询 -->
      <el-input
        v-model="id"
        @keyup.enter.native="loadPostID"
        placeholder="请输入用户id"
        suffix-icon="el-icon-search"
        style="width: 200px; margin-left: 5px"
      ></el-input>
      <!--      查询按钮-->
      <el-button type="primary" style="margin-left: 5px" @click="loadPostID"
        >用户ID查询</el-button
      >
      <!--      重置按钮-->
      <el-button type="success" @click="resetParam">重置</el-button>
      <!--      新增按钮-->
      <el-button type="primary" style="margin-left: 5px" @click="add"
        >新增</el-button
      >
    </div>

    <!--    列表展示-->
    <el-table
      :data="titles"
      :header-cell-style="{ background: '#D3D3D3FF', color: '#555555' }"
    >
      <el-table-column prop="id" label="id" width="180"> </el-table-column>
      <el-table-column prop="title" label="头衔名称" width="160">
      </el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="gmtModified" label="修改时间" width="180">
      </el-table-column>
      <el-table-column
        prop="isPrivate"
        label="是否公开"
        width="90"
      >
      <template v-slot:default="{ row }">
        <el-switch
          v-model="row.isPrivate"
          active-color="#ff4949"
          inactive-color="#13ce66"
          @change="handleSwitchChange(row)"
        >
        </el-switch>
      </template>
        
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="这是一段内容确定删除吗？"
            @confirm="del(scope.row.id)"
            style="margin-left: 5px"
          >
            <el-button slot="reference" size="small" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页  -->
    <div class="pagination-wrapper">
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="头衔信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
      :before-close="handleClose"
    >
      <!--      新增学院信息表单-->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="头衔名称" prop="title">
          <el-col :span="15">
            <el-input v-model="form.title"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="是否公开" prop="isPrivate">
          <el-radio-group v-model="form.isPrivate">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { del } from "@/api/titleManagement/Delete";
import { mod } from "@/api/titleManagement/Mod";
import { save } from "@/api/titleManagement/Save";
import { query } from "@/api/titleManagement/query";
import { modifyPublic } from "@/api/titleManagement/modifyPublic";
import { queryId } from "@/api/titleManagement/queryId";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "titleManagement",
  data() {
    return {
      num: 1,
      titles: [],
      page: 1,
      limit: 5,
      total: 0,
      searchForName: "",
      id: "",
      centerDialogVisible: false,
      isPrivate:false,
      form: {
        isPrivate: 1,
        title: "",
      },
      rules: {
        title: [{ required: true, message: "请输入头衔名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    //重置表单
    resetForm() {
      this.$refs.form.resetFields();
    },

    //新增按钮
    add() {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    //修改
    mod(row) {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        //赋值到表单
        this.id = row.id;
        this.form.title = row.title;
        this.form.isPrivate = row.isPrivate;
      });
    },
    //删除
    del(id) {
      del(id).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
          });
        }
      });
    },
    //修改
    doMod() {
      mod(this.form).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
          this.resetForm();
        } else {
          this.$message({
            message: "修改失败",
            type: "error",
          });
        }
      });
    },
    //修改头衔的公开状态
    handleSwitchChange(row){
      modifyPublic(row.id).then((res) => {
        if (res.code == 20000) {
          this.$message({
            message: "公开状态修改成功",
            type: "success",
          });
        }else {
          this.$message({
            message: "公开状态修改失败",
            type: "error",
          });
        }
      })
    },
    //增加
    doSave() {
      save(this.form).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "增加成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
          this.resetForm();
        } else {
          this.$message({
            message: "增加失败",
            type: "error",
          });
        }
      });
    },
    //提交数据到后台
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            //如果有id则修改
            this.doMod();
          } else {
            //否则增加
            this.doSave();
          }
        } else {
          this.$message({
            message: "操作失败",
            type: "error",
          });
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.page = 1;
      this.limit = val;
      this.loadPost();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loadPost();
    },
    resetParam() {
      this.searchForName = "";
      this.loadPost();
    },
    loadPost() {
      query(this.page, this.limit, { name: this.searchForName }).then((res) => {
        if (res.code === 20000) {
          this.titles = res.data.list.records;
          this.total = res.data.list.total;
        } else {
          alert("获取数据失败");
        }
      });
    },
    loadPostID(){
        queryId(this.id).then((res) => {
            if (res.code == 20000) {
               this.titles = res.data.titles
            }
        })
    }
  },
  beforeMount() {
    this.loadPost();
  },
};
</script>

<style scoped>
.pagination-wrapper {
  position: fixed;
  bottom: 0;
  width: 100%;
  /*background-color: #f5f5f5;*/
  padding: 10px 20px;
  /*box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);*/
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  max-width: 800px; /* 设置最大宽度以避免在大屏幕上过分拉伸 */
  margin: 0 30%; /* 居中显示分页组件 */
}
</style>
